const path = require('path')
const configureWebpack = {}

if (process.env.NODE_ENV !== 'development') {
	configureWebpack.externals = {
		'vue': 'Vue',
		'vue-router': 'VueRouter',
		'vuex': 'Vuex'
	}
}

process.env.VUE_APP_VERSION = `v${require('./package.json').version}`
process.env.VUE_APP_IS_PROD = JSON.stringify(process.env.NODE_ENV === 'production' ? 1 : 0)

module.exports = {
	productionSourceMap: process.env.NODE_ENV === 'development',
	assetsDir: 'static',
	devServer: {
		proxy: {
			'/common/api': {
				target: 'http://10.30.30.25:38080',
				changeOrigin: true
			}
		}
	},
	configureWebpack,
	chainWebpack: (config) => {
		// 重写 images 规则
		const imagesRule = config.module.rule('images').test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)

		imagesRule.exclude.add(path.join(__dirname, 'src/assets/svg'))

		imagesRule.use('url-loader').loader('url-loader').options({
			limit: 4096,
			fallback: {
				loader: 'file-loader',
				options: {
					name: 'static/images/[name].[hash:8].[ext]'
				}
			}
		})

		// 重写 svg 规则
		const svgRule = config.module.rule('svg').include.add(path.join(__dirname, 'src/assets/svg')).end()

		svgRule.uses.clear()

		svgRule.use('svg-sprite-loader').loader('svg-sprite-loader').options({
			name: '[name]',
			prefixze: true
		})

		svgRule.use('svgo-loader').loader('svgo-loader').options({
			plugins: [
				{ removeTitle: true },
				{ convertColors: { shorthex: true } },
				{ convertPathData: true },
				{ cleanupAttrs: true },
				{ removeComments: true },
				{ removeDesc: true },
				{ removeUselessDefs: true },
				{ removeEmptyAttrs: true },
				{ removeHiddenElems: true },
				{ removeEmptyText: true }
			]
		})
	}
}